# coding:utf-8

import os
import platform
from ctypes import *


HS_TABLE_NAME_LEN = 128
MAX_OWNER_LEN = 16
MAX_PASSWORD_LEN = 16
HS_FIELD_NAME_LEN = 64
HS_FIELD_DISP_NAME_LEN = 48
IPSTRING_LENGTH = 16
USERNAME_LENGTH = 64
PASSWORD_LENGTH = 32
MAX_TABLE_PATH = 256
MAX_TABLE_NAME = 256
MAX_PATH = 260
MAX_THREAD = 256
PREPOSTFIX_LENGTH = 16
TABLENAME_LENGTH = 128
SYS_CONCEPT_DIC = 'SYS_CONCEPT_DIC'

TPI_ERR_BUFFER =            -1107   # 申请的缓冲区太小
TPI_ERR_GETCOLDATALEN =     -3023   # 取字段长度失败
TPI_ERR_GETCOLDATA =        -3024   # 取字段值失败
TPI_ERR_FIELD_NORIGHTS =    -6019   # 该字段没有权限
TPI_ERR_FIELD_NAME =        -6020   # 字段名称不存在
TPI_ERR_FIELD_INDEX =       -6021   # 字段序号不存在

TPI_HCON = c_int32
TPI_HRECORDSET = c_int32
TPI_HEVENT = c_int32
HTABLE = c_int
HCOL = c_int
HSTIME32 = c_uint
INT64 = c_int64

HSD_SOB = 0x400+0x0e
HSD_LOB = 0x400+0x0f
HSD_DOB = 0x400+0x10

HSD_VIRTUAL = 0x400+0x0d
HSD_TNAME = 0x400+0x13
HSD_TANAME = 0x400+0x14
HSD_RECORDID = 0x400+0x15
HSD_RELEVANT = 0x400+0x16
HSD_SNAPSHOT = 0x400+0x17
HSD_SMART = 0x400+0x1b
HSD_XML_VIRTUAL = 0x400+0x1c
HSD_NULL = 0x400+0x1d

class HS_Query(Structure):
    _fields_ = [('hQueryId', c_int),
                ('nHitCount', c_int),
                ('nFreq', c_int),]

class HS_TABLE_HEAD(Structure):
    _fields_ = [('version', c_int),
                ('subVersion', c_int),
                ('tableName', c_wchar*HS_TABLE_NAME_LEN),
                ('tableAliasName', c_wchar*HS_TABLE_NAME_LEN),
                ('tableOwner', c_wchar*MAX_OWNER_LEN),
                ('iExtType', c_int16),
                ('iExtFlag', c_int16),
                ('FileCacheNum', c_byte*9),
                ('dump1', c_char*27),
                ('appendCount', c_int),
                ('lastAutoID', c_int),
                ('tablePassword', c_wchar*MAX_PASSWORD_LEN),
                ('type', c_int),
                ('dump1', c_char*40),
                ('style', c_int),
                ('createTime', c_wchar*16),
                ('modifyTime', c_wchar*16),
                ('languadCode', c_int),
                ('fieldNum', c_int),
                ('recNum', c_int),]

class HS_TABLE_FIELD(Structure):
    _fields_ = [('name', c_wchar*HS_FIELD_NAME_LEN),
                ('aliasName', c_wchar*HS_FIELD_NAME_LEN),
                ('dispName', c_wchar*HS_FIELD_DISP_NAME_LEN),
                ('code', c_uint16, 4),
                ('data', c_uint16, 12),
                ('index', c_uint16),
                ('specProperty', c_int32),
                ('modifiedCount', c_uint32),
                ('lastIndexRec', c_uint32),
                ('type', c_int32),
                ('len', c_int32),
                ('fieldOffset', c_int32),
                ('indexkeyLen', c_int32),
                ('indexType', c_uint16),
                ('indexProperty', c_uint16),
                ('indexRootBlkNo', c_int),]

class HS_FIELD_MAP_ITEM(Structure):
    _fields_ = [('srcNum', c_int32),
                ('mapNum', c_int32),
                ('weight', c_uint32),
                ('property', c_uint32),
                ('maxcount', c_uint32),
                ('dump', c_uint32*3),]

class _HS_TABLE_HEAD_Z3950(Structure):
    _fields_ = [('strHostName', c_wchar * 64),
                ('wdHostPort', c_uint32),
                ('strDBName', c_wchar * 64),
                ('strRecordType', c_wchar * 16),
                ('strConName', c_wchar * MAX_OWNER_LEN),
                ('strConPassWord', c_wchar * MAX_PASSWORD_LEN),
                ('strGroupID', c_wchar * MAX_OWNER_LEN),
                ('bNamedResultSet', c_uint32),]

class TPI_IP(Structure):
    _fields_ = [('szIp', c_wchar*IPSTRING_LENGTH),]

class TPI_LOGIN_PARA(Structure):
    _fields_ = [('szUserName', c_wchar * USERNAME_LENGTH),
                ('szPassWord', c_wchar * PASSWORD_LENGTH),
                ('UserIp', TPI_IP),]

class TPI_DATA_INNER(Structure):
    _fields_ = [('lSize', c_int),]

class TPI_DATA(Structure):
    _fields_ = [('lSize', c_int),
                ('pData', c_wchar_p),]

class TPI_VAR_LONGS_INNER(Structure):
    _fields_ = [('lCount', c_int),]

class TPI_VAR_LONGS(Structure):
    _fields_ = [('lCount', c_int),
                ('lData', py_object),]

class TPI_TABLE_INFO_INNER(Structure):
    _fields_ = [('szTablePath', c_wchar * MAX_TABLE_PATH),
                ('lTableSize', c_int),
                ('TableHead', HS_TABLE_HEAD),]

class TPI_TABLE_INFO(Structure):
    _fields_ = [('szTablePath', c_wchar * MAX_TABLE_PATH),
                ('lTableSize', c_int),
                ('TableHead', HS_TABLE_HEAD),
                ('pTableField', py_object),]

class TPI_FILE_REC_OUTPUT_INNER(Structure):
    _fields_ = [('nlength', c_int),
                ('szTable', c_wchar * MAX_TABLE_NAME),
                ('szPackFile', c_wchar * MAX_TABLE_PATH),
                ('nStartRec', c_int),
                ('nEndRec', c_int),]

class TPI_FILE_REC_OUTPUT(Structure):
    _fields_ = [('nlength', c_int),
                ('szTable', c_wchar * MAX_TABLE_NAME),
                ('szPackFile', c_wchar * MAX_TABLE_PATH),
                ('nStartRec', c_int),
                ('nEndRec', c_int),
                ('sqlWhere', c_wchar_p),]

class TPI_ADD_RECORD_INNER(Structure):
    _fields_ = [('szTableName', c_wchar * MAX_TABLE_NAME),
                ('lDataLen', c_int),]

class TPI_ADD_RECORD(Structure):
    _fields_ = [('szTableName', c_wchar * MAX_TABLE_NAME),
                ('lDataLen', c_int),
                ('pRecordBuf', c_wchar_p),]

class TPI_BULK_SET_RECORD(Structure):
    _fields_ = [('hQuery', HS_Query),
                ('vRow', TPI_VAR_LONGS),]

class TPI_DISKINFO(Structure):
    _fields_ = [('fTotalSize', c_float),
                ('fFreeSize', c_float),
                ('DiskName', c_char),]

class TPI_DIRINFO(Structure):
    _fields_ = [('szName', c_wchar * 256),
                ('bIsFile', c_int),]

class TPI_COPYFILE(Structure):
    _fields_ = [('szSrcPath', c_wchar * MAX_PATH),
                ('szDesPath', c_wchar * MAX_PATH),]

class TPI_INI_READWRITE_INNER(Structure):
    _fields_ = [('szFile', c_wchar * MAX_PATH),
                ('szSection', c_wchar * 32),
                ('szKey', c_wchar * 32),
                ('lLen', c_int),]

class TPI_INI_READWRITE(Structure):
    _fields_ = [('szFile', c_wchar * MAX_PATH),
                ('szSection', c_wchar * 32),
                ('szKey', c_wchar * 32),
                ('lLen', c_int),
                ('pData', c_wchar_p),]

class RECORDSET(Structure):
    _fields_ = [('hQuery', HS_Query),
                ('lCurRec', c_int),
                ('hCon', TPI_HCON),
                ('dwOptions', c_uint),]

class MemOpt(Structure):
    _fields_ = [('DeviceBuffer', c_int),
                ('RecordBuffer', c_int),
                ('LxtBuffer', c_int),
                ('LinkBuffer', c_int),
                ('TextIdBuffer', c_int),
                ('GIdBuffer', c_int),
                ('MemSizePerRoad', c_int),
                ('TextMergeRoad', c_int),
                ('TextCountPerRoad', c_int),
                ('CharCountPerRoad', c_int),
                ('FullFieldTextMergeRoad', c_int),
                ('FullFieldTextCountPerRoad', c_int),
                ('nQueryCacheCountForLocalTable', c_int),
                ('nQueryCacheCountForTeleTable', c_int),
                ('nQueryCacheCountForUspTable', c_int),
                ('importDataIndexOn', c_int),
                ('logDebugMsg', c_int),
                ('loadLocalDict', c_int),
                ('loadUnicodeDict', c_int),]

class CONFIG_PARAM(Structure):
    _fields_ = [('memParam', MemOpt),
                ('m_nBTreeBuf', c_int),
                ('m_nRecordBuf', c_int),
                ('m_nDataFileBuf', c_int),
                ('m_IsCreateSysTable', c_int),
                ('m_IsFixDatabase', c_int),
                ('nClusterEnable', c_int),
                ('isClusterCenterNode', c_int),
                ('isClusterNode', c_int),
                ('szClusterCenterIP', c_wchar * 16),
                ('nClusterCenterPort', c_int),
                ('isClusterSecCenterNode', c_int),
                ('szClusterSecCenterIP', c_wchar * 16),
                ('nClusterSecCenterPort', c_int),]

class USP_FIELD(Structure):
    _fields_ = [('iFieldIndex', c_int),
                ('nFieldType', c_int),
                ('strFieldName', c_wchar * 64),]

class USP_FIELDINFO_RET_INNER(Structure):
    _fields_ = [('nFiedCount', c_int),]

class USP_FIELDINFO_RET(Structure):
    _fields_ = [('nFiedCount', c_int),
                ('FieldInfo', py_object),]

class TPI_TABLE_STORAGESPACE_INNER(Structure):
    _fields_ = [('tableName', c_wchar * HS_TABLE_NAME_LEN),
                ('name', c_wchar * HS_TABLE_NAME_LEN),
                ('type', c_short),
                ('lColCount', c_int),
                ('lColLen', c_int),]

class TPI_TABLE_STORAGESPACE(Structure):
    _fields_ = [('tableName', c_wchar * HS_TABLE_NAME_LEN),
                ('name', c_wchar * HS_TABLE_NAME_LEN),
                ('type', c_short),
                ('lColCount', c_int),
                ('lColLen', c_int),
                ('szCol', c_wchar_p),]

class TPI_OPEN_RECORDSET(Structure):
    _fields_ = [('pszSQL', c_wchar_p),
                ('nExecType', c_int),
                ('nFlag', c_int),
                ('nWaitSecond', c_int),
                ('nRecCount', c_int),
                ('szPrefix', c_wchar*PREPOSTFIX_LENGTH),
                ('szPostfix', c_wchar * PREPOSTFIX_LENGTH),
                ('szReserved', c_wchar * 64),]

class TPI_EVENT_RUNTYPE(Structure):
    _fields_ = [('lType', c_int),
                ('lTime', c_int),
                ('lDate', c_int),]

class TPI_SERVER_SYSTEMINFO(Structure):
    _fields_ = [('HostIP', TPI_IP),
                ('lPort', c_int),
                ('lVersion', c_int),
                ('lMaxConNum', c_int),
                ('lCurConNum', c_int),
                ('lThreadNum', c_int),
                ('lMaxThreadNum', c_int),
                ('lBaseID', c_int),
                ('lSysMemory', c_uint),
                ('lAvailableMemory', c_uint),
                ('lTableNum', c_int),
                ('lViewNum', c_int),
                ('lUserCount', c_int),
                ('tStartupTime', HSTIME32),
                ('bRunning', c_bool),
                ('lLogDays', c_int),
                ('lLogLevel', c_int),
                ('lOperateTimeOut', c_int),
                ('lConnectTimeOut', c_int),
                ('szSvrPath', c_wchar * MAX_PATH),
                ('szCurrentTime', c_wchar * 18),]

class TPI_SERVER_SYSTEMINFO_EX(Structure):
    _fields_ = [('nSize', c_int),
                ('sysInfo', TPI_SERVER_SYSTEMINFO),
                ('lCurThreadNum', c_int),
                ('lCanUseThreadNum', c_int),
                ('dwThreadFlag', c_uint * MAX_THREAD),
                ('szServerVer', c_wchar * 16),
                ('szServerBuildDate', c_wchar * 16),
                ('szHotstarVer', c_wchar * 16),
                ('szHotstarBuildDate', c_wchar * 16),
                ('lTotalCmdNum', INT64),
                ('szUsingPort', c_wchar * 32),
                ('szReserved', c_wchar * 256),]

class HS_ACCOUNT_DBNAME(Structure):
    _fields_ = [('lUser', c_int),
                ('szName', c_wchar * USERNAME_LENGTH),
                ('szDbName', c_wchar * TABLENAME_LENGTH),]

class TPI_ACCOUNT_DBRIGHT_INNER(Structure):
    _fields_ = [('nSize', c_int),
                ('userDB', HS_ACCOUNT_DBNAME),]

class TPI_ACCOUNT_DBRIGHT(Structure):
    _fields_ = [('nSize', c_int),
                ('userDB', HS_ACCOUNT_DBNAME),
                ('Mark', c_wchar_p),]

workpath = os.getcwd()
dllpath = os.path.split(os.path.realpath(__file__))[0]
os.chdir(dllpath)
if platform.system() == 'Windows':
    kbaselib = cdll.LoadLibrary('.\\TpiClientU.dll')
elif platform.system() == 'Linux':
    kbaselib = cdll.LoadLibrary('./libtpiclientu.so')
os.chdir(workpath)


def TPI_GetErrorMsg(nErrorCode, pErrBuf, nbuflen):
    GetErrorMsg = kbaselib.TPI_GetErrorMsg
    GetErrorMsg.argtypes = [c_int, c_wchar_p, c_int]
    GetErrorMsg.restype = c_int
    return GetErrorMsg(nErrorCode, pErrBuf, nbuflen)

def TPI_WriteLog(pErrinfo):
    WriteLog = kbaselib.TPI_WriteLog
    WriteLog.argtypes = [c_wchar_p]
    WriteLog.restype = c_int
    return WriteLog(pErrinfo)

def TPI_GetLogFilePath(pPath, nLen):
    buflen = c_int(nLen)
    GetLogFilePath = kbaselib.TPI_GetLogFilePath
    GetLogFilePath.argtypes = [c_wchar_p, POINTER(c_int)]
    GetLogFilePath.restype = c_int
    return GetLogFilePath(pPath, byref(buflen))

def TPI_OpenCon(pServerHost, nPort, loginPara, pErrorCode, pExType = None):
    OpenCon = kbaselib.TPI_OpenCon
    OpenCon.argtypes = [c_wchar_p, c_int, TPI_LOGIN_PARA, POINTER(c_int), POINTER(c_int)]
    OpenCon.restype = TPI_HCON
    return OpenCon(pServerHost, nPort, loginPara, pErrorCode, pExType)

def TPI_OpenCon2(pServerHost, nPort, loginPara, pErrorCode, pExType = None):
    OpenCon2 = kbaselib.TPI_OpenCon2
    OpenCon2.argtypes = [c_wchar_p, c_int, POINTER(TPI_LOGIN_PARA), POINTER(c_int), POINTER(c_int)]
    OpenCon2.restype = TPI_HCON
    return OpenCon2(pServerHost, nPort, loginPara, pErrorCode, pExType)

def TPI_Reconnect(hCon):
    Reconnect = kbaselib.TPI_Reconnect
    Reconnect.argtypes = [TPI_HCON]
    Reconnect.restype = c_int
    return Reconnect(hCon)

def TPI_CloseCon(hCon):
    CloseCon = kbaselib.TPI_CloseCon
    CloseCon.argtypes = [TPI_HCON]
    CloseCon.restype = c_int
    return CloseCon(hCon)

def TPI_IsValidCon(hCon):
    IsValidCon = kbaselib.TPI_IsValidCon
    IsValidCon.argtypes = [TPI_HCON]
    IsValidCon.restype = c_int
    return IsValidCon(hCon)

def TPI_IsActiveCon(hCon):
    IsActiveCon = kbaselib.TPI_IsActiveCon
    IsActiveCon.argtypes = [TPI_HCON]
    IsActiveCon.restype = c_int
    return IsActiveCon(hCon)

def TPI_RefreshCon(hCon, nTimeout):
    RefreshCon = kbaselib.TPI_RefreshCon
    RefreshCon.argtypes = [TPI_HCON, c_int]
    RefreshCon.restype = c_int
    return RefreshCon(hCon, nTimeout)

def TPI_CheckServerState(hCon):
    CheckServerState = kbaselib.TPI_CheckServerState
    CheckServerState.argtypes = [TPI_HCON]
    CheckServerState.restype = c_int
    return CheckServerState(hCon)

def TPI_ExecSQL(hCon, pSQL, bUnicode = False):
    ExecSQL = kbaselib.TPI_ExecSQL
    ExecSQL.argtypes = [TPI_HCON, c_wchar_p, c_bool]
    ExecSQL.restype = c_int
    return ExecSQL(hCon, pSQL, bUnicode)

def TPI_ExecMgrSql(hCon, pSQL, pEventHandle = None, bUnicode = False):
    ExecMgrSql = kbaselib.TPI_ExecMgrSql
    ExecMgrSql.argtypes = [TPI_HCON, c_wchar_p, POINTER(TPI_HEVENT), c_bool]
    ExecMgrSql.restype = c_int
    return ExecMgrSql(hCon, pSQL, pEventHandle, bUnicode)

def TPI_CheckSql(hCon, pSQL, bUnicode = False):
    CheckSql = kbaselib.TPI_CheckSql
    CheckSql.argtypes = [TPI_HCON, c_wchar_p, c_bool]
    CheckSql.restype = c_int
    return CheckSql(hCon, pSQL, bUnicode)

def TPI_GetTableCount(hCon):
    GetTableCount = kbaselib.TPI_GetTableCount
    GetTableCount.argtypes = [TPI_HCON]
    GetTableCount.restype = c_int
    return GetTableCount(hCon)

def TPI_GetTableNameBySn(hCon, nSn, pTableName):
    GetTableNameBySn = kbaselib.TPI_GetTableNameBySn
    GetTableNameBySn.argtypes = [TPI_HCON, c_int, c_wchar_p]
    GetTableNameBySn.restype = c_int
    return GetTableNameBySn(hCon, nSn, pTableName)

def TPI_TableNameExists(hCon, pTableName):
    TableNameExists = kbaselib.TPI_TableNameExists
    TableNameExists.argtypes = [TPI_HCON, c_wchar_p]
    TableNameExists.restype = c_int
    return TableNameExists(hCon, pTableName)

def TPI_GetViewCount(hCon):
    GetViewCount = kbaselib.TPI_GetViewCount
    GetViewCount.argtypes = [TPI_HCON]
    GetViewCount.restype = c_int
    return GetViewCount(hCon)

def TPI_GetViewNameBySn(hCon, nSn, pViewName):
    GetViewNameBySn = kbaselib.TPI_GetViewNameBySn
    GetViewNameBySn.argtypes = [TPI_HCON, c_int, c_wchar_p]
    GetViewNameBySn.restype = c_int
    return GetViewNameBySn(hCon, nSn, pViewName)

def TPI_ViewNameExists(hCon, pViewName):
    ViewNameExists = kbaselib.TPI_ViewNameExists
    ViewNameExists.argtypes = [TPI_HCON, c_wchar_p]
    ViewNameExists.restype = c_int
    return ViewNameExists(hCon, pViewName)

def TPI_GetViewSQL(hCon, pViewName, buf, buflen, bUnicode = False):
    GetViewSQL = kbaselib.TPI_GetViewSQL
    GetViewSQL.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p, POINTER(c_int), c_bool]
    GetViewSQL.restype = c_int
    return GetViewSQL(hCon, pViewName, buf, buflen, bUnicode)

def TPI_RefreshTable(hCon):
    RefreshTable = kbaselib.TPI_RefreshTable
    RefreshTable.argtypes = [TPI_HCON]
    RefreshTable.restype = c_int
    return RefreshTable(hCon)

def TPI_OpenRecordSet(hCon, sql, pErrorCode, bUnicode = False):
    OpenRecordSet = kbaselib.TPI_OpenRecordSet
    OpenRecordSet.argtypes = [TPI_HCON, c_wchar_p, POINTER(c_int), c_bool]
    OpenRecordSet.restype = TPI_HRECORDSET
    return OpenRecordSet(hCon, sql, pErrorCode, bUnicode)

def TPI_OpenRecordSetEx(hCon, sql, pErrorCode, nExecType= 0, nFlag = 0, nWaitSecond = 0, bUnicode = False):
    OpenRecordSetEx = kbaselib.TPI_OpenRecordSetEx
    OpenRecordSetEx.argtypes = [TPI_HCON, c_wchar_p, POINTER(c_int), c_int, c_int, c_int, c_bool]
    OpenRecordSetEx.restype = TPI_HRECORDSET
    return OpenRecordSetEx(hCon, sql, pErrorCode, nExecType, nFlag, nWaitSecond, bUnicode)

def TPI_OpenRecordSet2(hCon, pInfo, pErrorCode, bUnicode = False):
    OpenRecordSet2 = kbaselib.TPI_OpenRecordSet2
    OpenRecordSet2.argtypes = [TPI_HCON, POINTER(TPI_OPEN_RECORDSET), POINTER(c_int), c_bool]
    OpenRecordSet2.restype = TPI_HRECORDSET
    return OpenRecordSet2(hCon, pInfo, pErrorCode, bUnicode)

def TPI_CloseRecordSet(hSet):
    CloseRecordSet = kbaselib.TPI_CloseRecordSet
    CloseRecordSet.argtypes = [TPI_HRECORDSET]
    CloseRecordSet.restype = c_int
    return CloseRecordSet(hSet)

def TPI_CloseRecordSetEx(hSet, nFlag = 0):
    CloseRecordSetEx = kbaselib.TPI_CloseRecordSetEx
    CloseRecordSetEx.argtypes = [TPI_HRECORDSET, c_int]
    CloseRecordSetEx.restype = c_int
    return CloseRecordSetEx(hSet, nFlag)

def TPI_ValidateQuery(hSet):
    ValidateQuery = kbaselib.TPI_ValidateQuery
    ValidateQuery.argtypes = [TPI_HRECORDSET]
    ValidateQuery.restype = c_int
    return ValidateQuery(hSet)

def TPI_IsValidRecordset(hSet):
    IsValidRecordset = kbaselib.TPI_IsValidRecordset
    IsValidRecordset.argtypes = [TPI_HRECORDSET]
    IsValidRecordset.restype = c_int
    return IsValidRecordset(hSet)

def TPI_SetHitWordMarkFlag(hSet, pPrefix, pPostfix):
    SetHitWordMarkFlag = kbaselib.TPI_SetHitWordMarkFlag
    SetHitWordMarkFlag.argtypes = [TPI_HRECORDSET, c_wchar_p, c_wchar_p]
    SetHitWordMarkFlag.restype = c_int
    return SetHitWordMarkFlag(hSet, pPrefix, pPostfix)

def TPI_GetRecordSet(hSet, RecordSet):
    GetRecordSet = kbaselib.TPI_GetRecordSet
    GetRecordSet.argtypes = [TPI_HRECORDSET, POINTER(RECORDSET)]
    GetRecordSet.restype = c_int
    return GetRecordSet(hSet, RecordSet)

def TPI_GetRecordSetCount(hSet):
    GetRecordSetCount = kbaselib.TPI_GetRecordSetCount
    GetRecordSetCount.argtypes = [TPI_HRECORDSET]
    GetRecordSetCount.restype = c_int
    return GetRecordSetCount(hSet)

def TPI_MoveFirst(hSet):
    MoveFirst = kbaselib.TPI_MoveFirst
    MoveFirst.argtypes = [TPI_HRECORDSET]
    MoveFirst.restype = c_bool
    return MoveFirst(hSet)

def TPI_MoveNext(hSet):
    MoveNext = kbaselib.TPI_MoveNext
    MoveNext.argtypes = [TPI_HRECORDSET]
    MoveNext.restype = c_bool
    return MoveNext(hSet)

def TPI_MovePrev(hSet):
    MovePrev = kbaselib.TPI_MovePrev
    MovePrev.argtypes = [TPI_HRECORDSET]
    MovePrev.restype = c_bool
    return MovePrev(hSet)

def TPI_MoveLast(hSet):
    MoveLast = kbaselib.TPI_MoveLast
    MoveLast.argtypes = [TPI_HRECORDSET]
    MoveLast.restype = c_bool
    return MoveLast(hSet)

def TPI_Move(hSet, lRec):
    Move = kbaselib.TPI_Move
    Move.argtypes = [TPI_HRECORDSET, c_int]
    Move.restype = c_bool
    return Move(hSet, lRec)

def TPI_IsEOF(hSet):
    IsEOF = kbaselib.TPI_IsEOF
    IsEOF.argtypes = [TPI_HRECORDSET]
    IsEOF.restype = c_bool
    return IsEOF(hSet)

def TPI_IsBOF(hSet):
    IsBOF = kbaselib.TPI_IsBOF
    IsBOF.argtypes = [TPI_HRECORDSET]
    IsBOF.restype = c_bool
    return IsBOF(hSet)

def TPI_AddNew(hSet):
    AddNew = kbaselib.TPI_AddNew
    AddNew.argtypes = [TPI_HRECORDSET]
    AddNew.restype = c_int
    return AddNew(hSet)

def TPI_Delete(hSet):
    Delete = kbaselib.TPI_Delete
    Delete.argtypes = [TPI_HRECORDSET]
    Delete.restype = c_int
    return Delete(hSet)

def TPI_Deletes(hSet, Count, Nos):
    Deletes = kbaselib.TPI_Deletes
    Deletes.argtypes = [TPI_HRECORDSET, c_int, POINTER(c_int)]
    Deletes.restype = c_int
    return Deletes(hSet, Count, Nos)

def TPI_Edit(hSet):
    Edit = kbaselib.TPI_Edit
    Edit.argtypes = [TPI_HRECORDSET]
    Edit.restype = c_int
    return Edit(hSet)

def TPI_Update(hSet, bUnicode = False):
    Update = kbaselib.TPI_Update
    Update.argtypes = [TPI_HRECORDSET, c_bool]
    Update.restype = c_int
    return Update(hSet, bUnicode)

def TPI_CancelUpdate(hSet):
    CancelUpdate = kbaselib.TPI_CancelUpdate
    CancelUpdate.argtypes = [TPI_HRECORDSET]
    CancelUpdate.restype = c_int
    return CancelUpdate(hSet)

def TPI_SetFieldValue(hSet, nColIndex, pValue, lbufLen):
    SetFieldValue = kbaselib.TPI_SetFieldValue
    SetFieldValue.argtypes = [TPI_HRECORDSET, c_int, c_wchar_p, c_int]
    SetFieldValue.restype = c_int
    return SetFieldValue(hSet, nColIndex, pValue, lbufLen)

def TPI_SetFieldValueByName(hSet, pFieldName, pValue, lbufLen):
    SetFieldValueByName = kbaselib.TPI_SetFieldValueByName
    SetFieldValueByName.argtypes = [TPI_HRECORDSET, c_wchar_p, c_wchar_p, c_int]
    SetFieldValueByName.restype = c_int
    return SetFieldValueByName(hSet, pFieldName, pValue, lbufLen)

def TPI_GetFieldValue(hSet, nColIndex, pValue, lbufLen):
    GetFieldValue = kbaselib.TPI_GetFieldValue
    GetFieldValue.argtypes = [TPI_HRECORDSET, c_int, c_wchar_p, c_int]
    GetFieldValue.restype = c_int
    return GetFieldValue(hSet, nColIndex, pValue, lbufLen)

def TPI_GetFieldValueEx(hSet, nColIndex, pValue, lbufLen, nFlag = 0):
    GetFieldValueEx = kbaselib.TPI_GetFieldValueEx
    GetFieldValueEx.argtypes = [TPI_HRECORDSET, c_int, c_wchar_p, POINTER(c_int), c_int]
    GetFieldValueEx.restype = c_int
    return GetFieldValueEx(hSet, nColIndex, pValue, lbufLen, nFlag)

def TPI_GetFieldValueByName(hSet, pFieldName, pValue, lbufLen):
    GetFieldValueByName = kbaselib.TPI_GetFieldValueByName
    GetFieldValueByName.argtypes = [TPI_HRECORDSET, c_wchar_p, c_wchar_p, c_int]
    GetFieldValueByName.restype = c_int
    return GetFieldValueByName(hSet, pFieldName, pValue, lbufLen)

def TPI_GetFieldValueByNameEx(hSet, pFieldName, pValue, lbufLen, nFlag = 0):
    GetFieldValueByNameEx = kbaselib.TPI_GetFieldValueByNameEx
    GetFieldValueByNameEx.argtypes = [TPI_HRECORDSET, c_wchar_p, c_wchar_p, POINTER(c_int), c_int]
    GetFieldValueByNameEx.restype = c_int
    return GetFieldValueByNameEx(hSet, pFieldName, pValue, lbufLen, nFlag)

def TPI_GetFieldLen(hSet, nFieldSn, pFieldLen):
    GetFieldLen = kbaselib.TPI_GetFieldLen
    GetFieldLen.argtypes = [TPI_HRECORDSET, c_int, POINTER(c_int)]
    GetFieldLen.restype = c_int
    return GetFieldLen(hSet, nFieldSn, pFieldLen)

def TPI_GetColDataLen(hCon, hSet, nColIndex):
    GetColDataLen = kbaselib.TPI_GetColDataLen
    GetColDataLen.argtypes = [TPI_HCON, TPI_HRECORDSET, c_int]
    GetColDataLen.restype = c_int
    return GetColDataLen(hCon, hSet, nColIndex)

def TPI_GetColDataLenEx(hCon, hSet, nColIndex, nFlag = 0):
    GetColDataLenEx = kbaselib.TPI_GetColDataLenEx
    GetColDataLenEx.argtypes = [TPI_HCON, TPI_HRECORDSET, c_int, c_int]
    GetColDataLenEx.restype = c_int
    return GetColDataLenEx(hCon, hSet, nColIndex, nFlag)

def TPI_GetColDataLenByFieldName(hCon, hSet, pFieldName):
    GetColDataLenByFieldName = kbaselib.TPI_GetColDataLenByFieldName
    GetColDataLenByFieldName.argtypes = [TPI_HCON, TPI_HRECORDSET, c_wchar_p]
    GetColDataLenByFieldName.restype = c_int
    return GetColDataLenByFieldName(hCon, hSet, pFieldName)

def TPI_GetColDataLenByFieldNameEx(hCon, hSet, pFieldName, nFlag = 0):
    GetColDataLenByFieldNameEx = kbaselib.TPI_GetColDataLenByFieldNameEx
    GetColDataLenByFieldNameEx.argtypes = [TPI_HCON, TPI_HRECORDSET, c_wchar_p, c_int]
    GetColDataLenByFieldNameEx.restype = c_int
    return GetColDataLenByFieldNameEx(hCon, hSet, pFieldName, nFlag)

def TPI_GetRecordTableName(hSet, pTableName):
    GetRecordTableName = kbaselib.TPI_GetRecordTableName
    GetRecordTableName.argtypes = [TPI_HRECORDSET, c_wchar_p]
    GetRecordTableName.restype = c_int
    return GetRecordTableName(hSet, pTableName)

def TPI_GetFieldCount(hSet):
    GetFieldCount = kbaselib.TPI_GetFieldCount
    GetFieldCount.argtypes = [TPI_HRECORDSET]
    GetFieldCount.restype = c_int
    return GetFieldCount(hSet)

def TPI_GetFieldInfo(hSet, pColumInfo):
    GetFieldInfo = kbaselib.TPI_GetFieldInfo
    GetFieldInfo.argtypes = [TPI_HRECORDSET, POINTER(HS_TABLE_FIELD)]
    GetFieldInfo.restype = c_int
    return GetFieldInfo(hSet, pColumInfo)

def TPI_GetFieldName(hSet, nFieldSn, pFieldName):
    GetFieldName = kbaselib.TPI_GetFieldName
    GetFieldName.argtypes = [TPI_HRECORDSET, c_int, c_wchar_p]
    GetFieldName.restype = c_int
    return GetFieldName(hSet, nFieldSn, pFieldName)

def TPI_GetRecordSetFieldName(hSet, fieldlist, bufLen, nFlag = 0):
    GetRecordSetFieldName = kbaselib.TPI_GetRecordSetFieldName
    GetRecordSetFieldName.argtypes = [TPI_HRECORDSET, c_wchar_p, POINTER(c_int), c_int]
    GetRecordSetFieldName.restype = c_int
    return GetRecordSetFieldName(hSet, fieldlist, bufLen, nFlag)

def TPI_GetFieldIndex(hCon, TableName, fieldName):
    GetFieldIndex = kbaselib.TPI_GetFieldIndex
    GetFieldIndex.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p]
    GetFieldIndex.restype = c_int
    return GetFieldIndex(hCon, TableName, fieldName)

def TPI_GetFieldType(hSet, nFieldSn, pFieldType):
    GetFieldType = kbaselib.TPI_GetFieldType
    GetFieldType.argtypes = [TPI_HRECORDSET, c_int, POINTER(c_int)]
    GetFieldType.restype = c_int
    return GetFieldType(hSet, nFieldSn, pFieldType)

def TPI_GetFieldTypeByName(hSet, pFieldName):
    GetFieldTypeByName = kbaselib.TPI_GetFieldTypeByName
    GetFieldTypeByName.argtypes = [TPI_HRECORDSET, c_wchar_p]
    GetFieldTypeByName.restype = c_int
    return GetFieldTypeByName(hSet, pFieldName)

def TPI_GetFieldEncodingByName(hSet, pFieldName):
    GetFieldEncodingByName = kbaselib.TPI_GetFieldEncodingByName
    GetFieldEncodingByName.argtypes = [TPI_HRECORDSET, c_wchar_p]
    GetFieldEncodingByName.restype = c_int
    return GetFieldEncodingByName(hSet, pFieldName)

def TPI_GetTableHead(hSet, pTableHead):
    GetTableHead = kbaselib.TPI_GetTableHead
    GetTableHead.argtypes = [TPI_HRECORDSET, POINTER(HS_TABLE_HEAD)]
    GetTableHead.restype = c_int
    return GetTableHead(hSet, pTableHead)

def TPI_GetTableSize(hSet):
    GetTableSize = kbaselib.TPI_GetTableSize
    GetTableSize.argtypes = [TPI_HRECORDSET]
    GetTableSize.restype = c_int
    return GetTableSize(hSet)

def TPI_GetTablePath(hSet, pTablePath):
    GetTablePath = kbaselib.TPI_GetTablePath
    GetTablePath.argtypes = [TPI_HRECORDSET, c_wchar_p]
    GetTablePath.restype = c_int
    return GetTablePath(hSet, pTablePath)

def TPI_CreateTable(hCon, pTableInfo):
    fieldnum = pTableInfo.TableHead.fieldNum
    fixedsize = sizeof(TPI_TABLE_INFO_INNER)
    variedsize = sizeof(HS_TABLE_FIELD) * fieldnum
    offset = fixedsize
    totalsize = fixedsize + variedsize
    tableinfo = create_string_buffer(totalsize)
    memmove(byref(tableinfo), byref(pTableInfo), fixedsize)
    for i in range(0, fieldnum):
        memmove(byref(tableinfo, offset), byref(pTableInfo.pTableField[i]), sizeof(HS_TABLE_FIELD))
        offset += sizeof(HS_TABLE_FIELD)
    CreateTable = kbaselib.TPI_CreateTable
    CreateTable.argtypes = [TPI_HCON, c_char_p]
    CreateTable.restype = c_int
    return CreateTable(hCon, tableinfo)

def TPI_CreateTable2(hCon, pTableInfo, pdbName):
    fieldnum = pTableInfo.TableHead.fieldNum
    fixedsize = sizeof(TPI_TABLE_INFO_INNER)
    variedsize = sizeof(HS_TABLE_FIELD) * fieldnum
    offset = fixedsize
    totalsize = fixedsize + variedsize
    tableinfo = create_string_buffer(totalsize)
    memmove(byref(tableinfo), byref(pTableInfo), fixedsize)
    for i in range(0, fieldnum):
        memmove(byref(tableinfo, offset), byref(pTableInfo.pTableField[i]), sizeof(HS_TABLE_FIELD))
        offset += sizeof(HS_TABLE_FIELD)
    CreateTable2 = kbaselib.TPI_CreateTable2
    CreateTable2.argtypes = [TPI_HCON, c_char_p, c_wchar_p]
    CreateTable2.restype = c_int
    return CreateTable2(hCon, tableinfo, pdbName)

def TPI_DeleteTable(hCon, tableName):
    DeleteTable = kbaselib.TPI_DeleteTable
    DeleteTable.argtypes = [TPI_HCON, c_wchar_p]
    DeleteTable.restype = c_int
    return DeleteTable(hCon, tableName)

def TPI_ModifyTable(hCon, pInfo, pModiItem, hEvent = 0):
    fieldnum = pInfo.TableHead.fieldNum
    fixedsize = sizeof(TPI_TABLE_INFO_INNER)
    variedsize = sizeof(HS_TABLE_FIELD) * fieldnum
    offset = fixedsize
    totalsize = fixedsize + variedsize
    tableinfo = create_string_buffer(totalsize)
    memmove(byref(tableinfo), byref(pInfo), fixedsize)
    for i in range(0, fieldnum):
        memmove(byref(tableinfo, offset), byref(pInfo.pTableField[i]), sizeof(HS_TABLE_FIELD))
        offset += sizeof(HS_TABLE_FIELD)
    ModifyTable = kbaselib.TPI_ModifyTable
    ModifyTable.argtypes = [TPI_HCON, c_char_p, POINTER(c_int), TPI_HEVENT]
    ModifyTable.restype = c_int
    return ModifyTable(hCon, tableinfo, pModiItem, hEvent)

def TPI_ImportTable(hCon, tablePath):
    ImportTable = kbaselib.TPI_ImportTable
    ImportTable.argtypes = [TPI_HCON, c_wchar_p]
    ImportTable.restype = c_int
    return ImportTable(hCon, tablePath)

def TPI_PackTable(hCon, tableName):
    PackTable = kbaselib.TPI_PackTable
    PackTable.argtypes = [TPI_HCON, c_wchar_p]
    PackTable.restype = c_int
    return PackTable(hCon, tableName)

def TPI_IndexTable(hCon, tableName, hEvent = None):
    IndexTable = kbaselib.TPI_IndexTable
    IndexTable.argtypes = [TPI_HCON, c_wchar_p, TPI_HEVENT]
    IndexTable.restype = c_int
    return IndexTable(hCon, tableName, hEvent)

def TPI_IndexTableField(hCon, tableName, pCol, hEvent = None):
    IndexTableField = kbaselib.TPI_IndexTableField
    IndexTableField.argtypes = [TPI_HCON, c_wchar_p, POINTER(c_int), TPI_HEVENT]
    IndexTableField.restype = c_int
    return IndexTableField(hCon, tableName, pCol, hEvent)

def TPI_OptimizeTable(hCon, tableName, hEvent = None):
    OptimizeTable = kbaselib.TPI_OptimizeTable
    OptimizeTable.argtypes = [TPI_HCON, c_wchar_p, TPI_HEVENT]
    OptimizeTable.restype = c_int
    return OptimizeTable(hCon, tableName, hEvent)

def TPI_RenameTable(hCon, pName, pNewName, pAliasName = None):
    RenameTable = kbaselib.TPI_RenameTable
    RenameTable.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p, c_wchar_p]
    RenameTable.restype = c_int
    return RenameTable(hCon, pName, pNewName, pAliasName)

def TPI_GetSvrInfo(hCon, pSvrInfo):
    GetSvrInfo = kbaselib.TPI_GetSvrInfo
    GetSvrInfo.argtypes = [TPI_HCON, POINTER(TPI_SERVER_SYSTEMINFO)]
    GetSvrInfo.restype = c_int
    return GetSvrInfo(hCon, pSvrInfo)

def TPI_GetServerInfo(hCon, pInfo, nLen):
    GetServerInfo = kbaselib.TPI_GetServerInfo
    GetServerInfo.argtypes = [TPI_HCON, POINTER(TPI_SERVER_SYSTEMINFO_EX), POINTER(c_int)]
    GetServerInfo.restype = c_int
    return GetServerInfo(hCon, pInfo, nLen)

def TPI_GetServerDiskInfo(hCon, pSvrDisk, pDiskCnt):
    GetServerDiskInfo = kbaselib.TPI_GetServerDiskInfo
    GetServerDiskInfo.argtypes = [TPI_HCON, POINTER(TPI_DISKINFO), POINTER(c_int)]
    GetServerDiskInfo.restype = c_int
    return GetServerDiskInfo(hCon, pSvrDisk, pDiskCnt)

def TPI_GetServerDirCount(hCon, pSrcPath):
    GetServerDirCount = kbaselib.TPI_GetServerDirCount
    GetServerDirCount.argtypes = [TPI_HCON, c_wchar_p]
    GetServerDirCount.restype = c_int
    return GetServerDirCount(hCon, pSrcPath)

def TPI_GetServerDirList(hCon, pPath, lStartPos, pCount, nDirFlag, pDirInfo):
    GetServerDirList = kbaselib.TPI_GetServerDirList
    GetServerDirList.argtypes = [TPI_HCON, c_wchar_p, c_int, POINTER(c_int), c_int, POINTER(TPI_DIRINFO)]
    GetServerDirList.restype = c_int
    return GetServerDirList(hCon, pPath, lStartPos, pCount, nDirFlag, pDirInfo)

def TPI_GetServerPath(hCon, pSvrPath):
    GetServerPath = kbaselib.TPI_GetServerPath
    GetServerPath.argtypes = [TPI_HCON, c_wchar_p]
    GetServerPath.restype = c_int
    return GetServerPath(hCon, pSvrPath)

def TPI_CopyFiles(hCon, pCopyFile):
    CopyFiles = kbaselib.TPI_CopyFiles
    CopyFiles.argtypes = [TPI_HCON, POINTER(TPI_COPYFILE)]
    CopyFiles.restype = c_int
    return CopyFiles(hCon, pCopyFile)

def TPI_DeleteFiles(hCon, pFile):
    DeleteFiles = kbaselib.TPI_DeleteFiles
    DeleteFiles.argtypes = [TPI_HCON, c_wchar_p]
    DeleteFiles.restype = c_int
    return DeleteFiles(hCon, pFile)

def TPI_FileExists(hCon, pFile):
    FileExists = kbaselib.TPI_FileExists
    FileExists.argtypes = [TPI_HCON, c_wchar_p]
    FileExists.restype = c_int
    return FileExists(hCon, pFile)

def TPI_CopyDir(hCon, pCopyFile):
    CopyDir = kbaselib.TPI_CopyDir
    CopyDir.argtypes = [TPI_HCON, POINTER(TPI_COPYFILE)]
    CopyDir.restype = c_int
    return CopyDir(hCon, pCopyFile)

def TPI_MakeDir(hCon, pDir):
    MakeDir = kbaselib.TPI_MakeDir
    MakeDir.argtypes = [TPI_HCON, c_wchar_p]
    MakeDir.restype = c_int
    return MakeDir(hCon, pDir)

def TPI_RemoveDir(hCon, pDir):
    RemoveDir = kbaselib.TPI_RemoveDir
    RemoveDir.argtypes = [TPI_HCON, c_wchar_p]
    RemoveDir.restype = c_int
    return RemoveDir(hCon, pDir)

def TPI_ReadStruct(hCon, pItem):
    fixedsize = sizeof(TPI_INI_READWRITE_INNER)
    variedsize = sizeof(c_wchar) * pItem.lLen
    offset = fixedsize
    totalsize = fixedsize + variedsize
    itembuf = create_string_buffer(totalsize)
    memmove(byref(itembuf), byref(pItem), fixedsize)
    ReadStruct = kbaselib.TPI_ReadStruct
    ReadStruct.argtypes = [TPI_HCON, c_char_p]
    ReadStruct.restype = c_int
    ret = ReadStruct(hCon, itembuf)
    memmove(byref(pItem), itembuf, fixedsize)
    pItem.pData = cast(byref(itembuf, offset), c_wchar_p)
    return ret

def TPI_WriteStruct(hCon, pItem):
    fixedsize = sizeof(TPI_INI_READWRITE_INNER)
    variedsize = sizeof(c_wchar) * pItem.lLen
    offset = fixedsize
    totalsize = fixedsize + variedsize
    itembuf = create_string_buffer(totalsize)
    memmove(byref(itembuf), byref(pItem), fixedsize)
    memmove(byref(itembuf, offset), pItem.pData, variedsize)
    WriteStruct = kbaselib.TPI_WriteStruct
    WriteStruct.argtypes = [TPI_HCON, c_char_p]
    WriteStruct.restype = c_int
    return WriteStruct(hCon, itembuf)

def TPI_ReadString(hCon, pItem):
    fixedsize = sizeof(TPI_INI_READWRITE_INNER)
    variedsize = sizeof(c_wchar) * pItem.lLen
    offset = fixedsize
    totalsize = fixedsize + variedsize
    itembuf = create_string_buffer(totalsize)
    memmove(byref(itembuf), byref(pItem), fixedsize)
    ReadString = kbaselib.TPI_ReadString
    ReadString.argtypes = [TPI_HCON, c_char_p]
    ReadString.restype = c_int
    ret = ReadString(hCon, itembuf)
    memmove(byref(pItem), itembuf, fixedsize)
    pItem.pData = cast(byref(itembuf, offset), c_wchar_p)
    return ret

def TPI_WriteString(hCon, pItem):
    fixedsize = sizeof(TPI_INI_READWRITE_INNER)
    variedsize = sizeof(c_wchar) * pItem.lLen
    offset = fixedsize
    totalsize = fixedsize + variedsize
    itembuf = create_string_buffer(totalsize)
    memmove(byref(itembuf), byref(pItem), fixedsize)
    memmove(byref(itembuf, offset), pItem.pData, variedsize)
    WriteString = kbaselib.TPI_WriteString
    WriteString.argtypes = [TPI_HCON, c_char_p]
    WriteString.restype = c_int
    return WriteString(hCon, itembuf)

def TPI_AppentRecText(hCon, strTableName, strPackFile):
    AppentRecText = kbaselib.TPI_AppentRecText
    AppentRecText.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p]
    AppentRecText.restype = TPI_HEVENT
    return AppentRecText(hCon, strTableName, strPackFile)

def TPI_AddRecData(hCon, pAddRec, bUnicode = False):
    fixedsize = sizeof(TPI_ADD_RECORD_INNER)
    variedsize = sizeof(c_wchar) * pAddRec.lDataLen
    offset = fixedsize
    totalsize = fixedsize + variedsize
    addinfo = create_string_buffer(totalsize)
    memmove(byref(addinfo), byref(pAddRec), fixedsize)
    memmove(byref(addinfo, offset), pAddRec.pRecordBuf, variedsize)
    AddRecData = kbaselib.TPI_AddRecData
    AddRecData.argtypes = [TPI_HCON, c_char_p, c_bool]
    AddRecData.restype = c_int
    return AddRecData(hCon, addinfo, bUnicode)

def TPI_AppentStart(hCon, pTableName):
    AppentStart = kbaselib.TPI_AppentStart
    AppentStart.argtypes = [TPI_HCON, c_wchar_p]
    AppentStart.restype = c_int
    return AppentStart(hCon, pTableName)

def TPI_AppentTable(hCon, pAddRec, bUnicode = False):
    fixedsize = sizeof(TPI_ADD_RECORD_INNER)
    variedsize = sizeof(c_wchar) * pAddRec.lDataLen
    offset = fixedsize
    totalsize = fixedsize + variedsize
    addinfo = create_string_buffer(totalsize)
    memmove(byref(addinfo), byref(pAddRec), fixedsize)
    memmove(byref(addinfo, offset), pAddRec.pRecordBuf, variedsize)
    AppentTable = kbaselib.TPI_AppentTable
    AppentTable.argtypes = [TPI_HCON, c_char_p, c_bool]
    AppentTable.restype = c_int
    return AppentTable(hCon, addinfo, bUnicode)

def TPI_AppentEnd(hCon, pTableName):
    AppentEnd = kbaselib.TPI_AppentEnd
    AppentEnd.argtypes = [TPI_HCON, c_wchar_p]
    AppentEnd.restype = c_int
    return AppentEnd(hCon, pTableName)

def TPI_ReadFile(hCon, strSourceFile, strDestFile):
    ReadFile = kbaselib.TPI_ReadFile
    ReadFile.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p]
    ReadFile.restype = c_int
    return ReadFile(hCon, strSourceFile, strDestFile)

def TPI_WriteFile(hCon, strSourceFile, strDestFile):
    WriteFile = kbaselib.TPI_WriteFile
    WriteFile.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p]
    WriteFile.restype = c_int
    return WriteFile(hCon, strSourceFile, strDestFile)

def TPI_ClearTableByName(hCon, tablename):
    ClearTableByName = kbaselib.TPI_ClearTableByName
    ClearTableByName.argtypes = [TPI_HCON, c_wchar_p]
    ClearTableByName.restype = c_int
    return ClearTableByName(hCon, tablename)

def TPI_CloseTable(hCon, tablename):
    CloseTable = kbaselib.TPI_CloseTable
    CloseTable.argtypes = [TPI_HCON, c_wchar_p]
    CloseTable.restype = c_int
    return CloseTable(hCon, tablename)

def TPI_OpenTable(hCon, tablename):
    OpenTable = kbaselib.TPI_OpenTable
    OpenTable.argtypes = [TPI_HCON, c_wchar_p]
    OpenTable.restype = c_int
    return OpenTable(hCon, tablename)

def TPI_BulkSetRecord(hCon, param, nCol, pdata, bUnicode = False):
    paramfixedsize = sizeof(HS_Query) + sizeof(TPI_VAR_LONGS_INNER)
    paramvariedsize = sizeof(c_int) * param.vRow.lCount
    paramoffset = sizeof(HS_Query)
    paramtotalsize = paramfixedsize + paramvariedsize
    paraminfo = create_string_buffer(paramtotalsize)
    memmove(byref(paraminfo), byref(param.hQuery), sizeof(HS_Query))
    memmove(byref(paraminfo, paramoffset), byref(param.vRow), sizeof(TPI_VAR_LONGS_INNER))
    paramoffset += sizeof(TPI_VAR_LONGS_INNER)
    for i in range(0, param.vRow.lCount):
        memmove(byref(paraminfo, paramoffset), byref(c_int(param.vRow.lData[i])), sizeof(c_int))
        paramoffset += sizeof(c_int)
    datafixedsize = sizeof(TPI_DATA_INNER)
    datavariedsize = sizeof(c_wchar) * pdata.lSize
    dataoffset = datafixedsize
    datatotalsize = datafixedsize + datavariedsize
    datainfo = create_string_buffer(datatotalsize)
    memmove(byref(datainfo), byref(pdata), datafixedsize)
    memmove(byref(datainfo, dataoffset), pdata.pData, datavariedsize)
    BulkSetRecord = kbaselib.TPI_BulkSetRecord
    BulkSetRecord.argtypes = [TPI_HCON, c_char_p, c_int, c_char_p, c_bool]
    BulkSetRecord.restype = c_int
    return BulkSetRecord(hCon, paraminfo, nCol, datainfo, bUnicode)

def TPI_BulkSetRecordEx(hCon, param, pCols, pdata, bUnicode = False):
    paramfixedsize = sizeof(HS_Query) + sizeof(TPI_VAR_LONGS_INNER)
    paramvariedsize = sizeof(c_int) * param.vRow.lCount
    paramoffset = sizeof(HS_Query)
    paramtotalsize = paramfixedsize + paramvariedsize
    paraminfo = create_string_buffer(paramtotalsize)
    memmove(byref(paraminfo), byref(param.hQuery), sizeof(HS_Query))
    memmove(byref(paraminfo, paramoffset), byref(param.vRow), sizeof(TPI_VAR_LONGS_INNER))
    paramoffset += sizeof(TPI_VAR_LONGS_INNER)
    for i in range(0, param.vRow.lCount):
        memmove(byref(paraminfo, paramoffset), byref(c_int(param.vRow.lData[i])), sizeof(c_int))
        paramoffset += sizeof(c_int)
    colfixedsize = sizeof(TPI_VAR_LONGS_INNER)
    colvariedsize = sizeof(c_int) * pCols.lCount
    coloffset = colfixedsize
    coltotalsize = colfixedsize + colvariedsize
    colinfo = create_string_buffer(coltotalsize)
    memmove(byref(colinfo), byref(pCols), colfixedsize)
    for i in range(0, pCols.lCount):
        memmove(byref(colinfo, coloffset), byref(c_int(pCols.lData[i])), sizeof(c_int))
        coloffset += sizeof(c_int)
    datafixedsize = sizeof(TPI_DATA_INNER)
    datavariedsize = sizeof(c_wchar) * pdata.lSize
    dataoffset = datafixedsize
    datatotalsize = datafixedsize + datavariedsize
    datainfo = create_string_buffer(datatotalsize)
    memmove(byref(datainfo), byref(pdata), datafixedsize)
    memmove(byref(datainfo, dataoffset), pdata.pData, datavariedsize)
    BulkSetRecordEx = kbaselib.TPI_BulkSetRecordEx
    BulkSetRecordEx.argtypes = [TPI_HCON, c_char_p, c_char_p, c_char_p, c_bool]
    BulkSetRecordEx.restype = c_int
    return BulkSetRecordEx(hCon, paraminfo, colinfo, datainfo, bUnicode)

def TPI_LockQuery(hCon, hSet, bLock):
    LockQuery = kbaselib.TPI_LockQuery
    LockQuery.argtypes = [TPI_HCON, TPI_HRECORDSET, c_bool]
    LockQuery.restype = c_int
    return LockQuery(hCon, hSet, bLock)

def TPI_WordSegment(hCon, srcBuf, destBuf, bufLen):
    WordSegment = kbaselib.TPI_WordSegment
    WordSegment.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p, POINTER(c_int)]
    WordSegment.restype = c_int
    return WordSegment(hCon, srcBuf, destBuf, bufLen)

def TPI_SetRecordCount(hSet, count):
    SetRecordCount = kbaselib.TPI_SetRecordCount
    SetRecordCount.argtypes = [TPI_HRECORDSET, c_int]
    SetRecordCount.restype = c_int
    return SetRecordCount(hSet, count)

def TPI_ClientCmd(hCon, retbuf, bufLen, pszCmd):
    ClientCmd = kbaselib.TPI_ClientCmd
    ClientCmd.argtypes = [TPI_HCON, c_wchar_p, POINTER(c_int), c_wchar_p]
    ClientCmd.restype = c_int
    return ClientCmd(hCon, retbuf, bufLen, pszCmd)

def TPI_ReloadDict(hCon, dictName):
    ReloadDict = kbaselib.TPI_ReloadDict
    ReloadDict.argtypes = [TPI_HCON, c_wchar_p]
    ReloadDict.restype = c_int
    return ReloadDict(hCon, dictName)

def TPI_KEGetDicts(hCon, xmlDicts, buflen):
    KEGetDicts = kbaselib.TPI_KEGetDicts
    KEGetDicts.argtypes = [TPI_HCON, c_wchar_p, POINTER(c_int)]
    KEGetDicts.restype = c_int
    return KEGetDicts(hCon,  xmlDicts, buflen)

def TPI_GetFieldTypeNameBySN(FieldNo, FieldName):
    GetFieldTypeNameBySN = kbaselib.TPI_GetFieldTypeNameBySN
    GetFieldTypeNameBySN.argtypes = [c_int, c_wchar_p]
    GetFieldTypeNameBySN.restype = c_int
    return GetFieldTypeNameBySN(FieldNo, FieldName)

def TPI_GetSNByFieldTypeName(FieldName):
    GetSNByFieldTypeName = kbaselib.TPI_GetSNByFieldTypeName
    GetSNByFieldTypeName.argtypes = [c_wchar_p]
    GetSNByFieldTypeName.restype = c_int
    return GetSNByFieldTypeName(FieldName)

def TPI_GetIndexTypeNameBySN(IndexNo, IndexName):
    GetIndexTypeNameBySN = kbaselib.TPI_GetIndexTypeNameBySN
    GetIndexTypeNameBySN.argtypes = [c_int, c_wchar_p]
    GetIndexTypeNameBySN.restype = c_int
    return GetIndexTypeNameBySN(IndexNo, IndexName)

def TPI_GetSNByIndexTypeName(IndexName):
    GetSNByIndexTypeName = kbaselib.TPI_GetSNByIndexTypeName
    GetSNByIndexTypeName.argtypes = [c_wchar_p]
    GetSNByIndexTypeName.restype = c_int
    return GetSNByIndexTypeName(IndexName)

def TPI_USPSetReplaceUrl(hCon, url):
    USPSetReplaceUrl = kbaselib.TPI_USPSetReplaceUrl
    USPSetReplaceUrl.argtypes = [TPI_HCON, c_wchar_p]
    USPSetReplaceUrl.restype = c_int
    return USPSetReplaceUrl(hCon, url)

def TPI_USPUrlGetContent(hCon, strTableName, pSearch, iResultID, iUrlID, piType, iOffset, buf, pibuflen):
    USPUrlGetContent = kbaselib.TPI_USPUrlGetContent
    USPUrlGetContent.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p, c_int, c_int, POINTER(c_int), c_int, c_wchar_p, POINTER(c_int)]
    USPUrlGetContent.restype = c_int
    return USPUrlGetContent(hCon, strTableName, pSearch, iResultID, iUrlID, byref(piType), iOffset, buf, byref(pibuflen))

def TPI_GetRelevantWords(hCon, curKeyWord, pszRelevantWords, nBufLen, Max_Relevant_Word_Num, SeperatorChar, WordFreqFollowed, pdbName = 'sys_dict_relevant'):
    GetRelevantWords = kbaselib.TPI_GetRelevantWords
    GetRelevantWords.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p, c_int, c_int, c_int, c_bool, c_wchar_p]
    GetRelevantWords.restype = c_int
    return GetRelevantWords(hCon, curKeyWord, pszRelevantWords, nBufLen, Max_Relevant_Word_Num, SeperatorChar, WordFreqFollowed, pdbName)

def TPI_GetRecordRelevant(hCon, hSet):
    GetRecordRelevant = kbaselib.TPI_GetRecordRelevant
    GetRecordRelevant.argtypes = [TPI_HCON, TPI_HRECORDSET]
    GetRecordRelevant.restype = c_int
    return GetRecordRelevant(hCon, hSet)

def TPI_GetQuerySQL(hCon, hSet, sql, nSqlLen, bUnicode = False):
    GetQuerySQL = kbaselib.TPI_GetQuerySQL
    GetQuerySQL.argtypes = [TPI_HCON, TPI_HRECORDSET, c_wchar_p, POINTER(c_int), c_bool]
    GetQuerySQL.restype = c_int
    return GetQuerySQL(hCon, hSet, sql, nSqlLen, bUnicode)

def TPI_TM_SimSearch(hCon, sql, vsm, pErrorCode, field, bUnicode = False):
    TM_SimSearch = kbaselib.TPI_TM_SimSearch
    TM_SimSearch.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p, POINTER(c_int), c_wchar_p, c_bool]
    TM_SimSearch.restype = TPI_HRECORDSET
    return TM_SimSearch(hCon, sql, vsm, pErrorCode, field, bUnicode)

def TPI_TM_DataSegment(hCon, pSource, lWeight, hDict, pBuff, nBuffLen):
    #TM_DataSegment = kbaselib.TPI_TM_DataSegment
    #TM_DataSegment.argtypes = [TPI_HCON, c_wchar_p, c_int, c_int, c_wchar_p, c_int]
    #TM_DataSegment.restype = c_int
    #return TM_DataSegment(hCon, pSource, lWeight, hDict, pBuff, nBuffLen)
    pass

def TPI_TM_LoadDict(hCon, pDictTable):
    TM_LoadDict = kbaselib.TPI_TM_LoadDict
    TM_LoadDict.argtypes = [TPI_HCON, c_wchar_p]
    TM_LoadDict.restype = c_int
    return TM_LoadDict(hCon, pDictTable)

def TPI_TM_UnLoadDict(hCon, hDict):
    TM_UnLoadDict = kbaselib.TPI_TM_UnLoadDict
    TM_UnLoadDict.argtypes = [TPI_HCON, c_int]
    TM_UnLoadDict.restype = c_int
    return TM_UnLoadDict(hCon, hDict)

def TPI_TM_GetDictHandle(hCon, pDictTable):
    TM_GetDictHandle = kbaselib.TPI_TM_GetDictHandle
    TM_GetDictHandle.argtypes = [TPI_HCON, c_wchar_p]
    TM_GetDictHandle.restype = c_int
    return TM_GetDictHandle(hCon, pDictTable)

def TPI_TM_DeleteDictHandle(hCon, hDict):
    TM_DeleteDictHandle = kbaselib.TPI_TM_DeleteDictHandle
    TM_DeleteDictHandle.argtypes = [TPI_HCON, c_int]
    TM_DeleteDictHandle.restype = c_int
    return TM_DeleteDictHandle(hCon, hDict)

def TPI_USP_Connect(hCon, strIP, dwPort, strConName, strConPassWord):
    USP_Connect = kbaselib.TPI_USP_Connect
    USP_Connect.argtypes = [TPI_HCON, c_wchar_p, c_uint, c_wchar_p, c_wchar_p]
    USP_Connect.restype = c_int
    return USP_Connect(hCon, strIP, dwPort, strConName, strConPassWord)

def TPI_USP_GetTableCount(hCon, hConUsp):
    USP_GetTableCount = kbaselib.TPI_USP_GetTableCount
    USP_GetTableCount.argtypes = [TPI_HCON, c_int]
    USP_GetTableCount.restype = c_int
    return USP_GetTableCount(hCon, hConUsp)

def TPI_USP_GetTableName(hCon, hConUsp, iTableID, strTableName, nLen):
    USP_GetTableName = kbaselib.TPI_USP_GetTableName
    USP_GetTableName.argtypes = [TPI_HCON, c_int, c_int, c_wchar_p, POINTER(c_int)]
    USP_GetTableName.restype = c_int
    return USP_GetTableName(hCon, hConUsp, iTableID, strTableName, byref(nLen))

def TPI_USP_GetFieldInfo(hCon, hConUsp, strTableName, pFieldInfo, nLen):
    fieldnum = 100
    fixedsize = sizeof(USP_FIELDINFO_RET_INNER)
    while True:
        variedsize = sizeof(HS_TABLE_FIELD) * fieldnum
        offset = fixedsize
        totalsize = fixedsize + variedsize
        uspfieldinfo = create_string_buffer(totalsize)
        buflen = c_int(totalsize)
        USP_GetFieldInfo = kbaselib.TPI_USP_GetFieldInfo
        USP_GetFieldInfo.argtypes = [TPI_HCON, c_int, c_wchar_p, c_char_p, POINTER(c_int)]
        USP_GetFieldInfo.restype = c_int
        ret = USP_GetFieldInfo(hCon, hConUsp, strTableName, uspfieldinfo, byref(nLen))
        if ret == TPI_ERR_BUFFER:
            fieldnum *= 2
            del (uspfieldinfo)
            continue
        elif ret >= 0:
            memmove(byref(pFieldInfo), byref(uspfieldinfo), fixedsize)
            fieldcount = uspfieldinfo.nFiedCount
            usptablefield = (HS_TABLE_FIELD * fieldcount)()
            for i in range(0, fieldcount):
                memmove(byref(usptablefield[i]), byref(uspfieldinfo, offset), sizeof(HS_TABLE_FIELD))
                offset += sizeof(HS_TABLE_FIELD)
            pFieldInfo.FieldInfo = tuple(usptablefield)
            bufLen = buflen.value
            return ret
        else:
            return ret

def TPI_USP_GetColInfo(hCon, hConUsp, strTableName, pRetBuff, nRetBuffLen):
    USP_GetColInfo = kbaselib.TPI_USP_GetColInfo
    USP_GetColInfo.argtypes = [TPI_HCON, c_int, c_wchar_p, POINTER(HS_TABLE_FIELD), c_int]
    USP_GetColInfo.restype = c_int
    return USP_GetColInfo(hCon, hConUsp, strTableName, pRetBuff, nRetBuffLen)

def TPI_USP_ConTable(hCon, path, strTableName, strHostIP, wdHostPort, strConName, strConPassWord, pDBName = 'USP'):
    USP_ConTable = kbaselib.TPI_USP_ConTable
    USP_ConTable.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p, c_wchar_p, c_ushort, c_wchar_p, c_wchar_p, c_wchar_p]
    USP_ConTable.restype = c_int
    return USP_ConTable(hCon, path, strTableName, strHostIP, wdHostPort, strConName, strConPassWord, pDBName)

def TPI_USP_IsTableExist(hTPICon, hUspCon, strTableName):
    USP_IsTableExist = kbaselib.TPI_USP_IsTableExist
    USP_IsTableExist.argtypes = [TPI_HCON, c_int, c_wchar_p]
    USP_IsTableExist.restype = c_int
    return USP_IsTableExist(hTPICon, hUspCon, strTableName)

def TPI_GetTableHandle(hSet):
    GetTableHandle = kbaselib.TPI_GetTableHandle
    GetTableHandle.argtypes = [TPI_HRECORDSET]
    GetTableHandle.restype = HTABLE
    return GetTableHandle(hSet)

def TPI_GetTableType(hCon, hTable):
    GetTableType = kbaselib.TPI_GetTableType
    GetTableType.argtypes = [TPI_HCON, HTABLE]
    GetTableType.restype = c_int
    return GetTableType(hCon, hTable)

def TPI_GetTableHitCount(hSet, pTableName):
    GetTableHitCount = kbaselib.TPI_GetTableHitCount
    GetTableHitCount.argtypes = [TPI_HRECORDSET, c_wchar_p]
    GetTableHitCount.restype = c_int
    return GetTableHitCount(hSet, pTableName)

def TPI_GetTableHostIP(hCon, hTable, strIP, nIPbufLen):
    GetTableHostIP = kbaselib.TPI_GetTableHostIP
    GetTableHostIP.argtypes = [TPI_HCON, HTABLE, c_wchar_p, POINTER(c_int)]
    GetTableHostIP.restype = c_int
    return GetTableHostIP(hCon, hTable, strIP, nIPbufLen)

def TPI_TableReConHost(hCon, hTable, strHostIPs, wdHostPort, strConName, strConPassWord):
    TableReConHost = kbaselib.TPI_TableReConHost
    TableReConHost.argtypes = [TPI_HCON, HTABLE, c_wchar_p, c_ushort,c_wchar_p, c_wchar_p]
    TableReConHost.restype = c_int
    return TableReConHost(hCon, hTable, strHostIPs, wdHostPort, strConName, strConPassWord)

def TPI_GetDBRight(hCon, ad, right_mode):
    GetDBRight = kbaselib.TPI_GetDBRight
    GetDBRight.argtypes = [TPI_HCON, POINTER(HS_ACCOUNT_DBNAME), POINTER(c_int)]
    GetDBRight.restype = c_int
    return GetDBRight(hCon, ad, right_mode)

def TPI_GetDBField(hCon, fieldlist, bufLen, ad):
    GetDBField = kbaselib.TPI_GetDBField
    GetDBField.argtypes = [TPI_HCON, c_wchar_p, POINTER(c_int), POINTER(HS_ACCOUNT_DBNAME)]
    GetDBField.restype = c_int
    return GetDBField(hCon, fieldlist, bufLen, ad)

def TPI_ValidateDBField(hCon, ad, pFieldList):
    ValidateDBField = kbaselib.TPI_ValidateDBField
    ValidateDBField.argtypes = [TPI_HCON, POINTER(HS_ACCOUNT_DBNAME), c_wchar_p]
    ValidateDBField.restype = c_int
    return ValidateDBField(hCon, ad, pFieldList)

def TPI_ValidateDBRight(hCon, ad, right_mode):
    ValidateDBRight = kbaselib.TPI_ValidateDBRight
    ValidateDBRight.argtypes = [TPI_HCON, POINTER(HS_ACCOUNT_DBNAME), c_int]
    ValidateDBRight.restype = c_int
    return ValidateDBRight(hCon, ad, right_mode)

def TPI_GetDatabaseCount(hCon):
    GetDatabaseCount = kbaselib.TPI_GetDatabaseCount
    GetDatabaseCount.argtypes = [TPI_HCON]
    GetDatabaseCount.restype = c_int
    return GetDatabaseCount(hCon)

def TPI_GetDatabaseName(hCon, dbNo, buf, nLen):
    GetDatabaseName = kbaselib.TPI_GetDatabaseName
    GetDatabaseName.argtypes = [TPI_HCON, c_int, c_wchar_p, c_int]
    GetDatabaseName.restype = c_int
    return GetDatabaseName(hCon, dbNo, buf, nLen)

def TPI_GetAllDatabaseName(hCon, pDataBuf, nDataBuffLen):
    buflen = c_int(nDataBuffLen)
    GetAllDatabaseName = kbaselib.TPI_GetAllDatabaseName
    GetAllDatabaseName.argtypes = [TPI_HCON, c_wchar_p, POINTER(c_int)]
    GetAllDatabaseName.restype = c_int
    return GetAllDatabaseName(hCon, pDataBuf, byref(buflen))

def TPI_GetTablesQueryInDB(hCon, pdbName, pErrorCode):
    GetTablesQueryInDB = kbaselib.TPI_GetTablesQueryInDB
    GetTablesQueryInDB.argtypes = [TPI_HCON, c_wchar_p, POINTER(c_int)]
    GetTablesQueryInDB.restype = c_int
    return GetTablesQueryInDB(hCon, pdbName, pErrorCode)

def TPI_GetTablesListInDB(hCon, pdbName, buf, nLen):
    GetTablesListInDB = kbaselib.TPI_GetTablesListInDB
    GetTablesListInDB.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p, POINTER(c_int)]
    GetTablesListInDB.restype = c_int
    return GetTablesListInDB(hCon, pdbName, buf, byref(nLen))

def TPI_ImportTable2(hCon, path, pdbName):
    ImportTable2 = kbaselib.TPI_ImportTable2
    ImportTable2.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p]
    ImportTable2.restype = c_int
    return ImportTable2(hCon, path, pdbName)

def TPI_MoveTable(hCon, path, pdbName):
    MoveTable = kbaselib.TPI_MoveTable
    MoveTable.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p]
    MoveTable.restype = c_int
    return MoveTable(hCon, path, pdbName)

def TPI_CreateZ3950Table(hCon, pTableInfo, p3950Head, pdbName):
    fieldnum = pTableInfo.TableHead.fieldNum
    fixedsize = sizeof(TPI_TABLE_INFO_INNER)
    variedsize = sizeof(HS_TABLE_FIELD) * fieldnum
    offset = fixedsize
    totalsize = fixedsize + variedsize
    tableinfo = create_string_buffer(totalsize)
    memmove(byref(tableinfo), byref(pTableInfo), fixedsize)
    for i in range(0, fieldnum):
        memmove(byref(tableinfo, offset), byref(pTableInfo.pTableField[i]), sizeof(HS_TABLE_FIELD))
        offset += sizeof(HS_TABLE_FIELD)
    CreateZ3950Table = kbaselib.TPI_CreateZ3950Table
    CreateZ3950Table.argtypes = [TPI_HCON, c_char_p, POINTER(_HS_TABLE_HEAD_Z3950), c_wchar_p]
    CreateZ3950Table.restype = c_int
    return CreateZ3950Table(hCon, tableinfo, p3950Head, pdbName)

def TPI_SetFieldMap(hCon, tablename, mapItemCount, pMapItem):
    SetFieldMap = kbaselib.TPI_SetFieldMap
    SetFieldMap.argtypes = [TPI_HCON, c_wchar_p, c_int, POINTER(HS_FIELD_MAP_ITEM)]
    SetFieldMap.restype = c_int
    return SetFieldMap(hCon, tablename, mapItemCount, pMapItem)

def TPI_GetFieldMap(hCon, tablename, pMapItemCount, pMapItem):
    GetFieldMap = kbaselib.TPI_GetFieldMap
    GetFieldMap.argtypes = [TPI_HCON, c_wchar_p, POINTER(c_int), POINTER(HS_FIELD_MAP_ITEM)]
    GetFieldMap.restype = c_int
    return GetFieldMap(hCon, tablename, pMapItemCount, pMapItem)

def TPI_PredictRelevantConcept(hCon, keyword, nNum, buf, nLen, ClusterIDFollowed = False, pdbName = SYS_CONCEPT_DIC, SeperatorChar = '$'):
    PredictRelevantConcept = kbaselib.TPI_PredictRelevantConcept
    PredictRelevantConcept.argtypes = [TPI_HCON, c_wchar_p, c_int, c_wchar_p, POINTER(c_int), c_bool, c_wchar_p, c_int]
    PredictRelevantConcept.restype = c_int
    sepchar = ord(SeperatorChar)
    return PredictRelevantConcept(hCon, keyword, nNum, buf, nLen, ClusterIDFollowed, pdbName, sepchar)

def TPI_GetTableInfoFromCache(hCon, pTableName, pInfo, bufLen):
    fieldnum = 100
    fixedsize = sizeof(TPI_TABLE_INFO_INNER)
    while True:
        variedsize = sizeof(HS_TABLE_FIELD) * fieldnum
        offset = fixedsize
        totalsize = fixedsize + variedsize
        tableinfo = create_string_buffer(totalsize)
        buflen = c_int(totalsize)
        GetTableInfoFromCache = kbaselib.TPI_GetTableInfoFromCache
        GetTableInfoFromCache.argtypes = [TPI_HCON, c_wchar_p, c_char_p, POINTER(c_int)]
        GetTableInfoFromCache.restype = c_int
        ret = GetTableInfoFromCache(hCon, pTableName, tableinfo, buflen)
        if ret == TPI_ERR_BUFFER:
            fieldnum *= 2
            del (tableinfo)
            continue
        elif ret >= 0:
            memmove(byref(pInfo), byref(tableinfo), fixedsize)
            fieldcount = pInfo.TableHead.fieldNum
            tablefield = (HS_TABLE_FIELD * fieldcount)()
            for i in range(0, fieldcount):
                memmove(byref(tablefield[i]), byref(tableinfo, offset), sizeof(HS_TABLE_FIELD))
                offset += sizeof(HS_TABLE_FIELD)
            pInfo.pTableField = tuple(tablefield)
            bufLen = buflen.value
            return ret
        else:
            return ret

def TPI_GrantAllDBField(hCon, ads, nbuflen, nCount):
    count = len(ads)
    fixedsize = sizeof(TPI_ACCOUNT_DBRIGHT_INNER) * count
    variedsize = 0
    for i in range(0, count):
        marksize = (len(ads[i].Mark) + 1) * sizeof(c_wchar)
        if marksize <= 4 * sizeof(c_wchar):
            marksize = 4 * sizeof(c_wchar)
        variedsize += marksize
    offset = 0
    totalsize = fixedsize + variedsize
    adsinfo = create_string_buffer(totalsize)
    for i in range(0, count):
        initoffset = offset
        memmove(byref(adsinfo, offset), byref(ads[i]), sizeof(TPI_ACCOUNT_DBRIGHT_INNER))
        offset += sizeof(TPI_ACCOUNT_DBRIGHT_INNER)
        marksize = len(ads[i].Mark) + 1
        if marksize <= 4:
            marksize = 4
        memmove(byref(adsinfo, initoffset), byref(c_int(sizeof(TPI_ACCOUNT_DBRIGHT_INNER) + marksize)), sizeof(c_int)) #nSize
        memmove(byref(adsinfo, offset), ads[i].Mark, marksize)
        offset += marksize
    GrantAllDBField = kbaselib.TPI_GrantAllDBField
    GrantAllDBField.argtypes = [TPI_HCON, c_char_p, c_int, c_int]
    GrantAllDBField.restype = c_int
    return GrantAllDBField(hCon, adsinfo, totalsize, nCount)

def TPI_GrantAllDBRight(hCon, ads, nbuflen, nCount):
    count = len(ads)
    fixedsize = sizeof(TPI_ACCOUNT_DBRIGHT_INNER) * count
    variedsize = 0
    for i in range(0, count):
        marksize = (len(ads[i].Mark) + 1) * sizeof(c_wchar)
        if marksize <= 4 * sizeof(c_wchar):
            marksize = 4 * sizeof(c_wchar)
        variedsize += marksize
    offset = 0
    totalsize = fixedsize + variedsize
    adsinfo = create_string_buffer(totalsize)
    for i in range(0, count):
        initoffset = offset
        memmove(byref(adsinfo, offset), byref(ads[i]), sizeof(TPI_ACCOUNT_DBRIGHT_INNER))
        offset += sizeof(TPI_ACCOUNT_DBRIGHT_INNER)
        marksize = len(ads[i].Mark) + 1
        if marksize <= 4:
            marksize = 4
        memmove(byref(adsinfo, initoffset), byref(c_int(sizeof(TPI_ACCOUNT_DBRIGHT_INNER) + marksize)), sizeof(c_int)) #nSize
        memmove(byref(adsinfo, offset), ads[i].Mark, marksize)
        offset += marksize
    GrantAllDBRight = kbaselib.TPI_GrantAllDBRight
    GrantAllDBRight.argtypes = [TPI_HCON, c_char_p, c_int, c_int]
    GrantAllDBRight.restype = c_int
    return GrantAllDBRight(hCon, adsinfo, totalsize, nCount)

def TPI_GetBLob(hSet, hcol, rec, offset, buf, bufLen):
    GetBLob = kbaselib.TPI_GetBLob
    GetBLob.argtypes = [TPI_HRECORDSET, HCOL, c_int, c_int, c_char_p, c_int]
    GetBLob.restype = c_int
    return GetBLob(hSet, hcol, rec, offset, buf, bufLen)

def TPI_SetBLob(hSet, hcol, rec, nType, buf, bufLen):
    SetBLob = kbaselib.TPI_SetBLob
    SetBLob.argtypes = [TPI_HRECORDSET, HCOL, c_int, c_int, c_char_p, c_int]
    SetBLob.restype = c_int
    return SetBLob(hSet, hcol, rec, nType, buf, bufLen)

def TPI_GetFieldTypeList(buf, bufLen):
    GetFieldTypeList = kbaselib.TPI_GetFieldTypeList
    GetFieldTypeList.argtypes = [c_wchar_p, POINTER(c_int)]
    GetFieldTypeList.restype = c_int
    return GetFieldTypeList(buf, bufLen)

def TPI_GetIndexTypeList(buf, bufLen):
    GetIndexTypeList = kbaselib.TPI_GetIndexTypeList
    GetIndexTypeList.argtypes = [c_wchar_p, POINTER(c_int)]
    GetIndexTypeList.restype = c_int
    return GetIndexTypeList(buf, bufLen)

def TPI_GetViewIndex(hCon, viewName):
    GetViewIndex = kbaselib.TPI_GetViewIndex
    GetViewIndex.argtypes = [TPI_HCON, c_wchar_p]
    GetViewIndex.restype = c_int
    return GetViewIndex(hCon, viewName)

def TPI_CreateView(hCon, pszName, pszSQL, bModify, bUnicode = False):
    CreateView = kbaselib.TPI_CreateView
    CreateView.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p, c_bool, c_bool]
    CreateView.restype = c_int
    return CreateView(hCon, pszName, pszSQL, bModify, bUnicode)

def TPI_DeleteView(hCon, viewName):
    DeleteView = kbaselib.TPI_DeleteView
    DeleteView.argtypes = [TPI_HCON, c_wchar_p]
    DeleteView.restype = c_int
    return DeleteView(hCon, viewName)

def TPI_GetHotStarConfigParam(hCon, Param):
    GetHotStarConfigParam = kbaselib.TPI_GetHotStarConfigParam
    GetHotStarConfigParam.argtypes = [TPI_HCON, POINTER(CONFIG_PARAM)]
    GetHotStarConfigParam.restype = c_int
    return GetHotStarConfigParam(hCon, Param)

def TPI_SetHotStarConfigParam(hCon, Param):
    SetHotStarConfigParam = kbaselib.TPI_SetHotStarConfigParam
    SetHotStarConfigParam.argtypes = [TPI_HCON, POINTER(CONFIG_PARAM)]
    SetHotStarConfigParam.restype = c_int
    return SetHotStarConfigParam(hCon, Param)

def TPI_CreateDataBase(hCon, dbName):
    CreateDataBase = kbaselib.TPI_CreateDataBase
    CreateDataBase.argtypes = [TPI_HCON, c_wchar_p]
    CreateDataBase.restype = c_int
    return CreateDataBase(hCon, dbName)

def TPI_GetTablesListInDBEx(hCon, pdbName, buf, nLen, nListType = 0):
    GetTablesListInDBEx = kbaselib.TPI_GetTablesListInDBEx
    GetTablesListInDBEx.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p, POINTER(c_int), c_int]
    GetTablesListInDBEx.restype = c_int
    return GetTablesListInDBEx(hCon, pdbName, buf, nLen, nListType)

def TPI_SetCacheFields(hSet, fieldBuf):
    SetCacheFields = kbaselib.TPI_SetCacheFields
    SetCacheFields.argtypes = [TPI_HRECORDSET, c_wchar_p]
    SetCacheFields.restype = c_int
    return SetCacheFields(hSet, fieldBuf)

def TPI_GetCacheFields(hSet, fieldBuf, nLen):
    GetCacheFields = kbaselib.TPI_GetCacheFields
    GetCacheFields.argtypes = [TPI_HRECORDSET, c_wchar_p, POINTER(c_int)]
    GetCacheFields.restype = c_int
    return GetCacheFields(hSet, fieldBuf, nLen)

def TPI_GetTableCountEx(hCon, nTableType):
    GetTableCountEx = kbaselib.TPI_GetTableCountEx
    GetTableCountEx.argtypes = [TPI_HCON, c_int]
    GetTableCountEx.restype = c_int
    return GetTableCountEx(hCon, nTableType)

def TPI_GetTableNameEx(hCon, nNo, nTableType, buf, nLen):
    GetTableNameEx = kbaselib.TPI_GetTableNameEx
    GetTableNameEx.argtypes = [TPI_HCON, c_int, c_int, c_wchar_p, POINTER(c_int)]
    GetTableNameEx.restype = c_int
    return GetTableNameEx(hCon, nNo, nTableType, buf, nLen)

def TPI_GetTablesFieldsList(hCon):
    GetTablesFieldsList = kbaselib.TPI_GetTablesFieldsList
    GetTablesFieldsList.argtypes = [TPI_HCON]
    GetTablesFieldsList.restype = c_int
    return GetTablesFieldsList(hCon)

def TPI_IsEndPage(hSet):
    IsEndPage = kbaselib.TPI_IsEndPage
    IsEndPage.argtypes = [TPI_HRECORDSET]
    IsEndPage.restype = c_int
    return IsEndPage(hSet)

def TPI_RefreshVQDataSet(hSet):
    RefreshVQDataSet = kbaselib.TPI_RefreshVQDataSet
    RefreshVQDataSet.argtypes = [TPI_HRECORDSET]
    RefreshVQDataSet.restype = c_int
    return RefreshVQDataSet(hSet)

def TPI_GetVQRecordCount(hSet):
    GetVQRecordCount = kbaselib.TPI_GetVQRecordCount
    GetVQRecordCount.argtypes = [TPI_HRECORDSET]
    GetVQRecordCount.restype = c_int
    return GetVQRecordCount(hSet)

def TPI_GetVQRecordCountByName(hSet, pDBName):
    GetVQRecordCountByName = kbaselib.TPI_GetVQRecordCountByName
    GetVQRecordCountByName.argtypes = [TPI_HRECORDSET, c_wchar_p]
    GetVQRecordCountByName.restype = c_int
    return GetVQRecordCountByName(hSet, pDBName)

def TPI_GetVQMaxRecordCount(hSet):
    GetVQMaxRecordCount = kbaselib.TPI_GetVQMaxRecordCount
    GetVQMaxRecordCount.argtypes = [TPI_HRECORDSET]
    GetVQMaxRecordCount.restype = c_int
    return GetVQMaxRecordCount(hSet)

def TPI_VQExpandTo(hSet, lRow):
    VQExpandTo = kbaselib.TPI_VQExpandTo
    VQExpandTo.argtypes = [TPI_HRECORDSET, c_int]
    VQExpandTo.restype = c_int
    return VQExpandTo(hSet, lRow)

def TPI_GetAllTableType(hCon, tableTypelist, bufLen):
    GetAllTableType = kbaselib.TPI_GetAllTableType
    GetAllTableType.argtypes = [TPI_HCON, c_wchar_p, POINTER(c_int)]
    GetAllTableType.restype = c_int
    return GetAllTableType(hCon, tableTypelist, bufLen)

def TPI_RefreshTableInfo(hCon, pTableName):
    RefreshTableInfo = kbaselib.TPI_RefreshTableInfo
    RefreshTableInfo.argtypes = [TPI_HCON, c_wchar_p]
    RefreshTableInfo.restype = c_int
    return RefreshTableInfo(hCon, pTableName)

def TPI_IsSingleTableMode(hSet):
    IsSingleTableMode = kbaselib.TPI_IsSingleTableMode
    IsSingleTableMode.argtypes = [TPI_HRECORDSET]
    IsSingleTableMode.restype = c_int
    return IsSingleTableMode(hSet)

def TPI_IsRecordSetReadOnly(hSet):
    IsRecordSetReadOnly = kbaselib.TPI_IsRecordSetReadOnly
    IsRecordSetReadOnly.argtypes = [TPI_HRECORDSET]
    IsRecordSetReadOnly.restype = c_int
    return IsRecordSetReadOnly(hSet)

def TPI_OutputRecToText(hCon, pOutputData, pFieldList, nFieldLen, bUnicode = False):
    sqllen = len(pOutputData.sqlWhere) + 1
    fixedsize = sizeof(TPI_FILE_REC_OUTPUT_INNER)
    variedsize = sizeof(c_wchar) * sqllen
    offset = fixedsize
    totalsize = fixedsize + variedsize
    outputinfo = create_string_buffer(totalsize)
    memmove(byref(outputinfo), byref(pOutputData), fixedsize)
    memmove(byref(outputinfo, offset), pOutputData.sqlWhere, variedsize)
    memmove(byref(outputinfo), byref(c_int(totalsize)), sizeof(c_int))
    OutputRecToText = kbaselib.TPI_OutputRecToText
    OutputRecToText.argtypes = [TPI_HCON, c_char_p, c_wchar_p, c_int, c_bool]
    OutputRecToText.restype = c_int
    return OutputRecToText(hCon, outputinfo, pFieldList, nFieldLen, bUnicode)

def TPI_NormalizeInput(pszInput, pszFreq, pszNormizedStr, buflen):
    NormalizeInput = kbaselib.TPI_NormalizeInput
    NormalizeInput.argtypes = [c_wchar_p, c_wchar_p, c_wchar_p, c_int]
    NormalizeInput.restype = c_int
    return NormalizeInput(pszInput, pszFreq, pszNormizedStr, buflen)

def TPI_NormalizeInputForSE(pszInput, pszFreq, pszNormizedStr, buflen):
    NormalizeInputForSE = kbaselib.TPI_NormalizeInputForSE
    NormalizeInputForSE.argtypes = [c_wchar_p, c_wchar_p, c_wchar_p, c_int]
    NormalizeInputForSE.restype = c_int
    return NormalizeInputForSE(pszInput, pszFreq, pszNormizedStr, buflen)

def TPI_QueryEvent(hCon, hevent):
    QueryEvent = kbaselib.TPI_QueryEvent
    QueryEvent.argtypes = [TPI_HCON, TPI_HEVENT]
    QueryEvent.restype = c_int
    return QueryEvent(hCon, hevent)

def TPI_GetTableHandle2(hCon, pTableName):
    GetTableHandle2 = kbaselib.TPI_GetTableHandle2
    GetTableHandle2.argtypes = [TPI_HCON, c_wchar_p]
    GetTableHandle2.restype = c_int
    return GetTableHandle2(hCon, pTableName)

def TPI_CreateUnionTable(hCon, pUnionTableName, pMainTable, pSubTables, pDBName):
    CreateUnionTable = kbaselib.TPI_CreateUnionTable
    CreateUnionTable.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p]
    CreateUnionTable.restype = c_int
    return CreateUnionTable(hCon, pUnionTableName, pMainTable, pSubTables, pDBName)

def TPI_GetUnionTableInfo(hCon, pUnionTableName, pBuff, nBuffLen):
    buflen = c_int(nBuffLen)
    GetUnionTableInfo = kbaselib.TPI_GetUnionTableInfo
    GetUnionTableInfo.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p, POINTER(c_int)]
    GetUnionTableInfo.restype = c_int
    return GetUnionTableInfo(hCon, pUnionTableName, pBuff, byref(buflen))

def TPI_GetSysDbList(hCon, dbList, nLen):
    GetSysDbList = kbaselib.TPI_GetSysDbList
    GetSysDbList.argtypes = [TPI_HCON, c_wchar_p, POINTER(c_int)]
    GetSysDbList.restype = c_int
    return GetSysDbList(hCon, dbList, nLen)

def TPI_GetUserOfGroup(hCon, userlist, bufLen, groupName):
    GetUserOfGroup = kbaselib.TPI_GetUserOfGroup
    GetUserOfGroup.argtypes = [TPI_HCON, c_wchar_p, POINTER(c_int), c_wchar_p]
    GetUserOfGroup.restype = c_int
    return GetUserOfGroup(hCon, userlist, bufLen, groupName)

def TPI_GetGroupOfUser(hCon, grouplist, bufLen, userName):
    GetGroupOfUser = kbaselib.TPI_GetGroupOfUser
    GetGroupOfUser.argtypes = [TPI_HCON, c_wchar_p, POINTER(c_int), c_wchar_p]
    GetGroupOfUser.restype = c_int
    return GetGroupOfUser(hCon, grouplist, bufLen, userName)

def TPI_GetDBNameofTable(hCon, tableName, DBName, len):
    GetDBNameofTable = kbaselib.TPI_GetDBNameofTable
    GetDBNameofTable.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p, POINTER(c_int)]
    GetDBNameofTable.restype = c_int
    return GetDBNameofTable(hCon, tableName, DBName, len)

def TPI_MarkKeyWordEx(hCon, srcBuf, srcLen, prefix, postfix, szBaseDict, szMarkDict, destBuf, destLen):
    MarkKeyWordEx = kbaselib.TPI_MarkKeyWordEx
    MarkKeyWordEx.argtypes = [TPI_HCON, c_wchar_p, c_int, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p, POINTER(c_int)]
    MarkKeyWordEx.restype = c_int
    return MarkKeyWordEx(hCon, srcBuf, srcLen, prefix, postfix, szBaseDict, szMarkDict, destBuf, destLen)

def TPI_SetKeyWordMarkPara(hSet, prefix, postfix, szBaseDict, szMarkDict):
    SetKeyWordMarkPara = kbaselib.TPI_SetKeyWordMarkPara
    SetKeyWordMarkPara.argtypes = [TPI_HRECORDSET, c_wchar_p, c_wchar_p, c_wchar_p, c_wchar_p]
    SetKeyWordMarkPara.restype = c_int
    return SetKeyWordMarkPara(hSet, prefix, postfix, szBaseDict, szMarkDict)

def TPI_BulkUpdate(hCon, pszCtlFileName):
    BulkUpdate = kbaselib.TPI_BulkUpdate
    BulkUpdate.argtypes = [TPI_HCON, c_wchar_p]
    BulkUpdate.restype = c_int
    return BulkUpdate(hCon, pszCtlFileName)

def TPI_ReIndexTable(hCon, hTable, nStartRec = 0):
    ReIndexTable = kbaselib.TPI_ReIndexTable
    ReIndexTable.argtypes = [TPI_HCON, HTABLE, c_int]
    ReIndexTable.restype = c_int
    return ReIndexTable(hCon, hTable, nStartRec)

def TPI_RandomSubSetQuery(hCon, hSet, rids, ridnum, hRandomSet):
    hrset = TPI_HRECORDSET(hRandomSet)
    RandomSubSetQuery = kbaselib.TPI_RandomSubSetQuery
    RandomSubSetQuery.argtypes = [TPI_HCON, TPI_HRECORDSET, POINTER(c_int), c_int, POINTER(TPI_HRECORDSET)]
    RandomSubSetQuery.restype = c_int
    return RandomSubSetQuery(hCon, hSet, rids, ridnum, byref(hrset))

def TPI_GetFieldValueByXml(hSet, nStartRec, nEndRec, nFlag, pField, pLen, buf, bufLen, bUnicode = False):
    GetFieldValueByXml = kbaselib.TPI_GetFieldValueByXml
    GetFieldValueByXml.argtypes = [TPI_HRECORDSET, c_int, c_int, c_int, c_wchar_p, c_wchar_p, c_wchar_p, POINTER(c_int), c_bool]
    GetFieldValueByXml.restype = c_int
    return GetFieldValueByXml(hSet, nStartRec, nEndRec, nFlag, pField, pLen, buf, bufLen, bUnicode)

def TPI_GetXmlRecordByMultiSQL(hCon, szSQL, buf, bufLen, nCount, pPrefix, pPostfix, bUnicode = False):
    GetXmlRecordByMultiSQL = kbaselib.TPI_GetXmlRecordByMultiSQL
    GetXmlRecordByMultiSQL.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p, POINTER(c_int), c_int, c_wchar_p, c_wchar_p, c_bool]
    GetXmlRecordByMultiSQL.restype = c_int
    return GetXmlRecordByMultiSQL(hCon, szSQL, buf, bufLen, nCount, pPrefix, pPostfix, bUnicode)

def TPI_Cluster_GetActiveNodes(hCon, buf, nLen):
    Cluster_GetActiveNodes = kbaselib.TPI_Cluster_GetActiveNodes
    Cluster_GetActiveNodes.argtypes = [TPI_HCON, c_wchar_p, POINTER(c_int)]
    Cluster_GetActiveNodes.restype = c_int
    return Cluster_GetActiveNodes(hCon, buf, nLen)

def TPI_GetCommunicationHandle(hCon, hFTSCon):
    hftscon = TPI_HCON(hFTSCon)
    GetCommunicationHandle = kbaselib.TPI_GetCommunicationHandle
    GetCommunicationHandle.argtypes = [TPI_HCON, POINTER(TPI_HCON)]
    GetCommunicationHandle.restype = c_int
    ret = GetCommunicationHandle(hCon, byref(hftscon))
    if ret >= 0:
        ret = hftscon.value
    return ret

def TPI_SessionAccess(hCon, tableName):
    SessionAccess = kbaselib.TPI_SessionAccess
    SessionAccess.argtypes = [TPI_HCON, c_wchar_p]
    SessionAccess.restype = c_int
    return SessionAccess(hCon, tableName)

def TPI_GetRowNativeId(hSet, lRow):
    GetRowNativeId = kbaselib.TPI_GetRowNativeId
    GetRowNativeId.argtypes = [TPI_HRECORDSET, c_int]
    GetRowNativeId.restype = c_int
    return GetRowNativeId(hSet, lRow)

def TPI_SetRemoteCluster(ip, port, tbname):
    SetRemoteCluster = kbaselib.TPI_SetRemoteCluster
    SetRemoteCluster.argtypes = [c_wchar_p, c_int, c_wchar_p]
    SetRemoteCluster.restype = c_int
    return SetRemoteCluster(ip, port, tbname)

def TPI_RefreshCluster(reload):
    RefreshCluster = kbaselib.TPI_RefreshCluster
    RefreshCluster.argtypes = [c_bool]
    RefreshCluster.restype = c_int
    return RefreshCluster(reload)

def TPI_GetStorageSpaceName(hCon, tablename, buf, nLen, nFlag = 0):
    GetStorageSpaceName = kbaselib.TPI_GetStorageSpaceName
    GetStorageSpaceName.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p, POINTER(c_int), c_int]
    GetStorageSpaceName.restype = c_int
    return GetStorageSpaceName(hCon, tablename, buf, nLen, nFlag)

def TPI_GetStorageSpaceInfo(hCon, tablename, SSName, info, nLen):
    colnum = 128
    fixedsize = sizeof(TPI_TABLE_STORAGESPACE_INNER)
    while True:
        variedsize = sizeof(c_wchar) * colnum
        offset = fixedsize
        totalsize = fixedsize + variedsize
        spaceinfo = create_string_buffer(totalsize)
        buflen = c_int(totalsize)
        GetStorageSpaceInfo = kbaselib.TPI_GetStorageSpaceInfo
        GetStorageSpaceInfo.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p, c_char_p, POINTER(c_int)]
        GetStorageSpaceInfo.restype = c_int
        ret = GetStorageSpaceInfo(hCon, tablename, SSName, spaceinfo, buflen)
        if ret == TPI_ERR_BUFFER:
            colnum *= 2
            del (spaceinfo)
            continue
        elif ret >= 0:
            memmove(byref(info), byref(spaceinfo), fixedsize)
            info.szCol = cast(byref(spaceinfo, offset), c_wchar_p)
            nLen = buflen.value
            return ret
        else:
            return ret

def TPI_GetIndexSpaceName(hCon, tablename, buf, nLen):
    GetIndexSpaceName = kbaselib.TPI_GetIndexSpaceName
    GetIndexSpaceName.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p, POINTER(c_int)]
    GetIndexSpaceName.restype = c_int
    return GetIndexSpaceName(hCon, tablename, buf, nLen)

def TPI_GetIndexSpaceCol(hCon, tablename, ISName, buf, nLen):
    GetIndexSpaceCol = kbaselib.TPI_GetIndexSpaceCol
    GetIndexSpaceCol.argtypes = [TPI_HCON, c_wchar_p, c_wchar_p, c_wchar_p, POINTER(c_int)]
    GetIndexSpaceCol.restype = c_int
    return GetIndexSpaceCol(hCon, tablename, ISName, buf, nLen)

def TPI_SetEvent(hCon, pInfo, lCmdCode, lSubCmdCode, param1, param1Len, param2, paran2Len):
    SetEvent = kbaselib.TPI_SetEvent
    SetEvent.argtypes = [TPI_HCON, POINTER(TPI_EVENT_RUNTYPE), c_int, c_int, c_wchar_p, c_int, c_wchar_p, c_int]
    SetEvent.restype = c_int
    return SetEvent(hCon, pInfo, lCmdCode, lSubCmdCode, param1, param1Len, param2, paran2Len)

def TPI_Initialize():
    Initialize = kbaselib.TPI_Initialize
    Initialize.argtypes = []
    Initialize.restype = c_int
    return Initialize()

def TPI_Uninitialize():
    Uninitialize = kbaselib.TPI_Uninitialize
    Uninitialize.argtypes = []
    Uninitialize.restype = c_int
    return Uninitialize()
